package com.xm.controller.web;

import com.xm.pojo.Dtu;
import com.xm.pojo.Sysuser;
import com.xm.service.DtuService;
import com.xm.util.JSONResult;
import com.xm.util.page.PageUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by wzl on 2018/4/3.
 */
@Controller
@RequestMapping("/web/dtu")
public class DtuController {
    @Resource
    private DtuService dtuService;

    /**
     * 跳转到dtu列表页面方法
     * @return
     */
    @RequestMapping(value="/toDtuList")
    public ModelAndView toDtuList(HttpServletRequest request){
        ModelAndView modelAndView=new ModelAndView("dtuList");
        Sysuser loginUser=(Sysuser)request.getSession().getAttribute("sysuser");
        modelAndView.addObject("sysuser",loginUser);
        return modelAndView;
    }

    /**
     * 新增dtu方法
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "/insertDtu", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertDtu(Dtu dtu) throws Exception {
        JSONResult jsonResult = new JSONResult();
        dtuService.insert(dtu);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 删除dtu方法
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "/deleteDtu", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult deleteDtu(Dtu dtu) throws Exception {
        JSONResult jsonResult = new JSONResult();
        dtuService.delete(dtu.getDtuId());
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 修改dtu方法
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "/updateDtu", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult updateDtu(Dtu dtu) throws Exception {
        JSONResult jsonResult = new JSONResult();
        dtuService.update(dtu);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 查询全部dtu方法
     *
     * @param dtu
     * @return
     */
    @RequestMapping(value = "/selectDtuList", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectDtuList(Dtu dtu) {
        JSONResult jsonResult = new JSONResult();
        List<Dtu> list = dtuService.selectList(dtu);
        jsonResult.setData(list);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 查询dtu详情方法
     *
     * @param dtu
     * @return
     */
    @RequestMapping(value = "/selectDtuDetail", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectDtuDetail(Dtu dtu) {
        JSONResult jsonResult = new JSONResult();
        Dtu dtuInfo = dtuService.selectOne(dtu);
        jsonResult.setData(dtuInfo);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 分页查询全部dtu方法
     *
     * @param dtu
     * @return
     */
    @RequestMapping(value = "/selectDtuPage", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectDtuPage(Dtu dtu) {
        JSONResult jsonResult=new JSONResult();
        List<Dtu> dtuList=dtuService.selectPageList(dtu);
        int total=dtuService.queryTotal(dtu);
        int length=dtu.getLength();
        int start=dtu.getStart();
        PageUtils pager = new PageUtils(dtuList, total, length,start);
        jsonResult.setData(pager);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 批量删除dtu
     */
    @RequestMapping(value ="/deleteDtus", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult deleteDtus(@RequestParam(value = "ids[]") Integer[] ids) {
        JSONResult jsonResult=new JSONResult();
        for(int i=0;i<ids.length;i++){
            dtuService.delete(ids[i]);
        }
        jsonResult.setSuccess(true);
        return jsonResult;
    }
}
